import { getuser, clearuser, setuser } from '../../utils/storage'
import { request, wxlogin } from '../../utils/https'
import {
  changeheadimg,
  oss
} from '../../utils/interfaces'
export default {
  data () {
    return {
      img: '',
      name: '',
      hotLessons: '',
      images: '',
      xiaoname: ''
    }
  },
  onShow () {
    this.start()
  },
  methods: {
    async start () {
      const { headimg, name, schooladdress } = await getuser('user')
      this.img = `${headimg}?x-oss-process=style/style160`
      this.name = name
      this.xiaoname = schooladdress.name
    },
    handleCell () {
      wx.navigateTo({
        url: '../counter/main'
      })
    },
    handleavter () {
      const { getkey, uploadFile, images } = this
      if (images.length >= 9) {
        return
      }
      wx.chooseImage({
        count: 1,
        sizeType: ['original', 'compressed'],
        sourceType: ['album'],
        async success (res) {
          const tempFilePaths = res.tempFilePaths
          if (tempFilePaths.length === 0) return
          const { OSSAccessKeyId, policy, signature } = await getkey()
          for (let v of tempFilePaths) {
            const hz = v.split('/')
            const name = `vue/student/${hz[hz.length - 1]}`
            await uploadFile(name, v, { OSSAccessKeyId, policy, signature })
          }
        }
      })
    },
    async getkey () {
      let code = await wxlogin()
      return request(oss, {
        data: {
          code
        }
      })
    },
    async uploadFile (name, filePath, {policy, OSSAccessKeyId, signature}) {
      const { changeheadimg } = this
      return new Promise((resolve, reject) => {
        wx.uploadFile({
          url: 'https://cdn-hterobot.oss-cn-qingdao.aliyuncs.com',
          filePath,
          name: 'file',
          formData: {
            // eslint-disable-next-line no-template-curly-in-string
            name: '${fileName}',
            key: name,
            OSSAccessKeyId,
            policy,
            signature
          },
          async success (res) {
            const k = `https://cdn.hterobot.com/${name}`
            await changeheadimg(k)
            resolve(res)
          },
          fail (err) {
            resolve(err)
          }
        })
      })
    },
    async changeheadimg (headimg) {
      const user = await getuser('user')
      let code = await wxlogin()
      try {
        await request(changeheadimg, {
          data: {
            code,
            ID: user.ID,
            headimg
          }
        })
        user.headimg = headimg
        await setuser(user)
        this.start()
      } catch (err) {
        wx.showModal({
          title: '提示',
          content: '服务器请求失败，点击确定重新加载',
          success: res => {
            if (res.confirm) {
              this.uploadFile()
            } else if (res.cancel) {
              console.log('用户点击取消')
            }
          }
        })
      }
    },
    handlemima () {
      wx.navigateTo({
        url: '../newpassword/main'
      })
    },
    async btn_click () {
      await clearuser()
      wx.reLaunch({
        url: '../home/main'
      })
    }
  }
}
